package com.sky.service.impl;

import com.github.pagehelper.Constant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.experimental.Helper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper  dishMapper;
    @Autowired
    private DishFlavorMapper  dishFlavorMapper;

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     */
    public void changestatus(Integer status, long id) {
        Dish dish = dishMapper.getDishById(id);
        dish.setStatus(status);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.changeStatus(dish);
    }

    /**
     * 根据id查询菜品
     * @param id
     */
    public DishVO getDishById(Integer id) {
        Dish dish = dishMapper.getDishById(id);
        List<DishFlavor> flavors = dishFlavorMapper.getDishFlavorBydishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 根据分类id查询菜品
     * @param id
     * @return
     */
    public List<Dish> getDishByCateId(Integer id) {
        return dishMapper.getDishByCateId(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteByIds(List<Integer> ids) {
        //删除关联的口味
        for (Integer id : ids) {
            dishFlavorMapper.deleteById(id);
        }
        dishMapper.deleteByIds(ids);
    }

    /**
     * 新增菜品
     * @param dishDTO
     */
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.DISABLE);
//        dish.setCreateTime(LocalDateTime.now());
//        dish.setUpdateTime(LocalDateTime.now());
//        dish.setCreateUser(BaseContext.getCurrentId());
//        dish.setUpdateUser(BaseContext.getCurrentId());
        //使用注解填充公共字段
        dishMapper.addDish(dish);
        //获取insert生成的主键值
        long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            flavors.forEach(DishFlavor->DishFlavor.setDishId(dishId));
            dishFlavorMapper.insertDishFlavor(flavors);
        }
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    public void editDish(DishDTO dishDTO) {
        Dish dish = dishMapper.getDishById(dishDTO.getId());
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.editDish(dish);
        //删除关联的口味在插入新的口味
        dishFlavorMapper.deleteById(Math.toIntExact(dish.getId()));
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            flavors.forEach(DishFlavor->DishFlavor.setDishId(dish.getId()));
            dishFlavorMapper.insertDishFlavor(flavors);
        }
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getDishFlavorBydishId(Math.toIntExact(d.getId()));

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
